import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import AutoImport from "unplugin-auto-import/vite";
import { resolve } from "path";

// https://vitejs.dev/config/
export default defineConfig({
  base: "./",
  server: {
    host: "localhost",
    port: 2022,
    proxy: {
      "/api": {
        target: "http://localhost:3000",
        rewrite: (path) => path.replace(/^\/api/, ""),
        changeOrigin: true,
      },
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 自定义的主题色
        additionalData: `
        @use "@/styles/element/light.scss" as *;
        `,
      },
    },
  },
  plugins: [
    Components({
      // 指定组件位置，默认是src/components
      dirs: ["src/components"],
      // ui库解析器
      resolvers: [
        ElementPlusResolver({
          importStyle: "sass",
        }),
      ],
      extensions: ["vue"],
      // 配置文件生成位置
      dts: "src/components.d.ts",
    }),
    AutoImport({
      resolvers: [
        ElementPlusResolver({
          // 自动引入修改主题色添加这一行，使用预处理样式
          importStyle: "sass",
        }),
      ],
      imports: ["vue", "vue-router", "vuex", "@vueuse/core"],
      // 可以选择auto-import.d.ts生成的位置，使用ts建议设置为'src/auto-import.d.ts'
      dts: "src/auto-import.d.ts",
    }),
    vue(),
  ],

  resolve: {
    alias: {
      "@": resolve(__dirname, "./src"), // 路径别名
    },
    extensions: [".js", ".json", ".ts"], // 使用路径别名时想要省略的后缀名，可以自己 增减
  },
});
